package com.example.cloud.controller.print;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.dao.basic.info.DiseasesRepository;
import com.example.cloud.dao.basic.info.SuggestRepository;
import com.example.cloud.dao.business.medical.*;
import com.example.cloud.dao.mongdb.basic.info.MongoProductRepository;
import com.example.cloud.dao.mongdb.basic.info.MongoProjectRepository;
import com.example.cloud.model.basic.Diseases;
import com.example.cloud.model.basic.Product;
import com.example.cloud.model.basic.Project;
import com.example.cloud.model.basic.Suggest;
import com.example.cloud.model.business.medical.Complaint;
import com.example.cloud.model.business.medical.ExecuteRecord;
import com.example.cloud.model.business.medical.FamilyHistoryRecords;
import com.example.cloud.model.business.medical.LifeCareAdvice;
import com.example.cloud.model.business.medical.PersonalHistoryRecords;
import com.example.cloud.model.business.medical.SkinCheck;
import com.example.cloud.model.business.medical.TreatPlanCourseRecord;
import com.example.cloud.model.business.reservation.Patient;
import com.example.cloud.model.customer.RefundRecord;
import com.example.cloud.model.system.Account;
import com.example.cloud.model.system.User;
import com.example.cloud.service.business.medical.ComplaintService;
import com.example.cloud.service.business.medical.LifeCareAdviceService;
import com.example.cloud.service.business.medical.PhysiologicalService;
import com.example.cloud.service.business.reservation.PatientService;
import com.example.cloud.service.customer.RefundRecordService;
import com.example.cloud.service.system.AccountService;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.DateUtils;
import com.example.cloud.utils.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by wangtao on 2018/1/26.
 */
@Controller
@RequestMapping("/print")
public class PrintController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PrintController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserService userService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private ComplaintService complaintService;

    @Autowired
    private FamilyHistoryRepository familyHistoryRepository;

    @Autowired
    private PersonalHistoryRepository personalHistoryRepository;

    @Autowired
    private SkinCheckRepository skinCheckRepository;

    @Autowired
    private SkinDiagnosisRepository skinDiagnosisRepository;

    @Autowired
    private DiseasesRepository diseasesRepository;

    @Autowired
    private SkinTypeRepository skinTypeRepository;

    @Autowired
    private TreatPlanCourseRecordRepository treatPlanCourseRecordRepository;

    @Autowired
    private ExecuteRecordRepository executeRecordRepository;

    @Autowired
    private MongoProductRepository mongoProductRepository;

    @Autowired
    private MongoProjectRepository mongoProjectRepository;

    @Autowired
    private TreatPlanProductRepository treatPlanProductRepository;

    @Autowired
    private TreatPlanCourseProductRepository treatPlanCourseProductRepository;

    @Autowired
    private TreatPlanHomeProductRepository treatPlanHomeProductRepository;

    @Autowired
    private LifeCareAdviceService lifeCareAdviceService;

    @Autowired
    private SuggestRepository suggestRepository;

    @Autowired
    private PhysiologicalService physiologicalService;

    @Autowired
    private RefundRecordService refundRecordService;

    @GetMapping("/medical")
    public ModelAndView printMedical(@RequestParam("patientId") Long patientId, @RequestParam("userId") Long userId) {
        ModelAndView view = new ModelAndView("printMedicalRecords");
        User user = userService.findOneFromCache(userId);
        view.addObject("username", user.getUsername());
        Account account = accountService.findOneFromCache(user.getAccountId());
        view.addObject("account", account);
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            patient.setCreationTime(new Date());
            view.addObject("patient", patient);
            if (patient.getBirthday() != null) {
                view.addObject("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            }
        }
        // 现病史
        List<Complaint> list1 = complaintService.findByPatientId(patientId, StateConstants.COMPLAINT_RECORD_TYPE_NEW);
        view.addObject("zhuDiseaseRecords", list1);
        // 既往史
        List<Complaint> list2 = complaintService.findByPatientId(patientId, StateConstants.COMPLAINT_RECORD_TYPE_OLD);
        view.addObject("jwDiseaseRecords", list2);
        // 家族史
        List<FamilyHistoryRecords> list3 = familyHistoryRepository.findByPatientIdAndDeleteFlag(patientId, 0);
        if (!list3.isEmpty()) {
            view.addObject("familyHistoryRecords", list3.get(0));
        }
        // 个人史
        List<PersonalHistoryRecords> list4 = personalHistoryRepository.findByPatientIdAndDeleteFlag(patientId, 0);
        if (!list4.isEmpty()) {
            view.addObject("personalHistoryRecords", list4.get(0));
        }
        // 治疗区情况
        List<SkinCheck> list5 = skinCheckRepository.findByPatientIdAndDeleteFlag(patientId, 0);
        if (!list5.isEmpty()) {
            view.addObject("skinChecks", list5);
        }
        //临床诊断==皮肤病诊断
        List<SkinDiagnosis> clinicalDiagnosisList = skinDiagnosisRepository.findByPatientIdAndSkinDiagnosisTypeAndDeleteFlag(patientId, StateConstants.SKIN_DIAGNOSIS_TYPE_DIAGNOSIS, 0);
        if (!clinicalDiagnosisList.isEmpty()) {
            SkinDiagnosis skinDiagnosis = clinicalDiagnosisList.get(clinicalDiagnosisList.size() - 1);//最新的
            String diagnosisResult = skinDiagnosis.getDiagnosisResult();//诊断结果
            if (StringUtils.isNotBlank(diagnosisResult)) {
                Long[] objects = StringUtils.stringToLongArray(diagnosisResult, ",");
                String clinicalDiagnosis = "";
                if (objects != null) {
                    List<Diseases> diseasesList = diseasesRepository.findByIdIn(objects);
                    for (Diseases diseases : diseasesList) {
                        String diseaseName = diseases.getName();
                        if (StringUtils.isNotBlank(clinicalDiagnosis)) {
                            clinicalDiagnosis += ",";
                        }
                        clinicalDiagnosis += diseaseName;
                    }
                }
                view.addObject("clinicalDiagnosis", clinicalDiagnosis);
            }
        }
        //问题皮肤
        List<SkinDiagnosis> problemSkinList = skinDiagnosisRepository.findByPatientIdAndSkinDiagnosisTypeAndDeleteFlag(patientId, StateConstants.SKIN_DIAGNOSIS_TYPE_PROBLEM_SKIN, 0);
        if (!problemSkinList.isEmpty()) {
            SkinDiagnosis skinDiagnosis = problemSkinList.get(problemSkinList.size() - 1);//最新的
            String diagnosisResult = skinDiagnosis.getDiagnosisResult();//诊断结果
            Long[] objects = StringUtils.stringToLongArray(diagnosisResult, ",");
            String problemSkins = "";
            if (objects != null) {
                List<Diseases> diseasesList = diseasesRepository.findByIdIn(objects);
                for (Diseases diseases : diseasesList) {
                    String diseaseName = diseases.getName();
                    if (StringUtils.isNotBlank(problemSkins)) {
                        problemSkins += ",";
                    }
                    problemSkins += diseaseName;
                }
            }
            view.addObject("problemSkins", problemSkins);
        }
        //皮肤年轻化
        List<SkinDiagnosis> youngs = skinDiagnosisRepository.findByPatientIdAndSkinDiagnosisTypeAndDeleteFlag(patientId, StateConstants.SKIN_DIAGNOSIS_TYPE_PROBLEM_YOUNGER, 0);
        if (!youngs.isEmpty()) {
            SkinDiagnosis skinDiagnosis = youngs.get(youngs.size() - 1);//最新的
            String diagnosisResult = skinDiagnosis.getDiagnosisResult();//诊断结果
            Long[] objects = StringUtils.stringToLongArray(diagnosisResult, ",");
            String youngSkinsStr = "";
            if (objects != null) {
                List<Diseases> diseasesList = diseasesRepository.findByIdIn(objects);
                for (Diseases diseases : diseasesList) {
                    String diseaseName = diseases.getName();
                    if (StringUtils.isNotBlank(youngSkinsStr)) {
                        youngSkinsStr += ",";
                    }
                    youngSkinsStr += diseaseName;
                }
            }
            view.addObject("youngSkins", youngSkinsStr);
        }
        // 皮肤分类
        List<SkinType> skinTypeList = skinTypeRepository.findByPatientIdAndDeleteFlagOrderByCreationTimeDesc(patientId, 0);
        if (!skinTypeList.isEmpty()) {
            SkinType skinType = skinTypeList.get(0);//最新一条
            String skinTypeResult = getType(skinType);
            view.addObject("skinTypeResult", skinTypeResult);
        }
        // 病程记录
        List<TreatPlanCourseRecord> courseRecordsList = treatPlanCourseRecordRepository.findByPatientIdAndDeleteFlag(patientId, 0);
        view.addObject("courseRecordsList", courseRecordsList);
        return view;
    }


    public String getType(SkinType skinType) {
        Integer result1 = skinType.getType1();
        Integer result2 = skinType.getType2();
        Integer result3 = skinType.getType3();
        Integer result4 = skinType.getType4();
        String text1 = convertTypeToText(1, result1);
        String text2 = convertTypeToText(2, result2);
        String text3 = convertTypeToText(3, result3);
        String text4 = convertTypeToText(4, result4);
        return text1 + "/" + text2 + "/" + text3 + "/" + text4;
    }

    public String convertTypeToText(int sort, Integer type) {
        if (type != null) {
            if (1 == sort && 1 == type) {
                return "油性(O)";
            } else if (1 == sort && 2 == type) {
                return "干性(D)";
            } else if (2 == sort && 1 == type) {
                return "敏感(S)";
            } else if (2 == sort && 2 == type) {
                return "耐受(R)";
            } else if (3 == sort && 1 == type) {
                return "色素(P)";
            } else if (3 == sort && 2 == type) {
                return "非色素(N)";
            } else if (4 == sort && 1 == type) {
                return "皱眉(W)";
            } else if (4 == sort && 2 == type) {
                return "紧致(T)";
            }
        }
        return "";
    }

    @GetMapping("/allAdviceExecute")
    public ModelAndView allAdviceExecute(@RequestParam("patientId") Long patientId, @RequestParam("userId") Long userId) {
        ModelAndView view = new ModelAndView("printAdviceExecute");
        Patient patient = patientService.findOne(patientId);
        view.addObject("patient", patient);
        List<ExecuteRecord> adviceExecutions = executeRecordRepository.findByPatientIdAndDeleteFlag(patientId, 0);
        Collections.sort(adviceExecutions, new Comparator<ExecuteRecord>() {
            @Override
            public int compare(ExecuteRecord o1, ExecuteRecord o2) {
                if (o1.getMedicalTime() != null && o2.getMedicalTime() != null) {
                    Calendar c1 = Calendar.getInstance();
                    Calendar c2 = Calendar.getInstance();
                    c1.setTime(o1.getMedicalTime());
                    c2.setTime(o2.getMedicalTime());
                    return c1.after(c2) ? 1 : -1;
                } else {
                    return -1;
                }
            }
        });
        view.addObject("adviceExecutions", adviceExecutions);
        return view;
    }

    @GetMapping("/courseRecords")
    public ModelAndView printCourseRecords(@RequestParam("patientId") Long patientId, @RequestParam("userId") Long userId) {
        ModelAndView view = new ModelAndView("printCourseRecords");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            view.addObject("medicalId", patient.getMedicalNumber());
            view.addObject("patientName", patient.getName());
            // 病程记录
            List<TreatPlanCourseRecord> courseRecordsList = treatPlanCourseRecordRepository.findByPatientIdAndDeleteFlag(patientId, 0);
            view.addObject("courseRecordsList", courseRecordsList);
        }
        return view;
    }

    @GetMapping("/adviceExecute")
    public ModelAndView partAdviceExecute(@RequestParam("ids") Long[] ids, @RequestParam("patientId") Long patientId, @RequestParam("userId") Long userId) {
        ModelAndView view = new ModelAndView("printAdviceExecute");
        Patient patient = patientService.findOne(patientId);
        view.addObject("patient", patient);
        List<ExecuteRecord> adviceExecutions = executeRecordRepository.findByIdInAndDeleteFlag(ids, 0);
        view.addObject("adviceExecutions", adviceExecutions);
        return view;
    }

    @GetMapping("/partCourseRecords")
    public ModelAndView printPartCourseRecords(@RequestParam Long[] ids, @RequestParam("patientId") Long patientId, @RequestParam("userId") Long userId) {
        ModelAndView view = new ModelAndView("printPartCourseRecords");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            view.addObject("patientName", patient.getName());
            view.addObject("medicalId", patient.getMedicalNumber());
        }
        List<TreatPlanCourseRecord> courseRecordsList = treatPlanCourseRecordRepository.findByIdIn(ids);
        if (!courseRecordsList.isEmpty()) {
            view.addObject("courseRecordsList", courseRecordsList);
            TreatPlanCourseRecord first = courseRecordsList.get(0);//最久的一条记录
            Long pid = first.getPatientId();
            Long id = first.getId();
            List<TreatPlanCourseRecord> courseRecords = treatPlanCourseRecordRepository.findByPatientIdAndIdLessThan(pid, id);//以前打印的数据
            if (!courseRecords.isEmpty()) {
                view.addObject("oldCourseRecords", courseRecords);
            }
        }
        return view;
    }

    @GetMapping("/partDoctorAdvice")
    public ModelAndView printPartDoctorAdvice(@RequestParam int type, @RequestParam Long[] ids) {
        String viewName = "";
        if (type == 1) {
            viewName = "printPartLongProjectDoctorAdvice";
        } else if (type == 2) {
            viewName = "printPartLongProductDoctorAdvice";
        } else if (type == 3) {
            viewName = "printPartTempDoctorAdvice";
        }
        ModelAndView view = new ModelAndView(viewName);
        List<ExecuteRecord> executionRecords = executeRecordRepository.findByIdInAndDeleteFlagOrderByCreationTimeDesc(ids, 0);
        view.addObject("executionRecords", executionRecords);
        return view;
    }

    /**
     * 打印处置笺
     *
     * @param patientId
     * @param data
     * @return
     */
    @GetMapping("/printDisposal")
    public ModelAndView printDisposal(@RequestParam("patientId") Long patientId, @RequestParam("data") String[] data) {
        ModelAndView view = new ModelAndView("printDisposal");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            JSONObject json = new JSONObject();
            json.put("name", patient.getName());
            json.put("sex", patient.getSex() == 1 ? "男" : "女");
            json.put("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            json.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
            json.put("medicalNumber", patient.getMedicalNumber());
            view.addObject("patient", json);
            List<String> data1 = new ArrayList<>();
            List<String> data2 = new ArrayList<>();
            for (int i = 0; i < data.length; i++) {
                if (i % 2 == 0) {
                    data1.add(data[i]);
                } else {
                    data2.add(data[i]);
                }
            }
            if (data1.size() > data2.size()) {
                data2.add("");
            } else if (data2.size() > data1.size()) {
                data1.add("");
            }
            view.addObject("data1", data1);
            view.addObject("data2", data2);
        }
        return view;
    }

    /**
     * 打印药品处方
     */
    @GetMapping("/printDrug")
    public ModelAndView printDrug(@RequestParam("username") String username, @RequestParam("patientId") Long patientId, @RequestParam String[] datas) {
        ModelAndView view = new ModelAndView("printDrugPrescription");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            JSONObject json = new JSONObject();
            json.put("name", patient.getName());
            json.put("username", username);
            json.put("sex", patient.getSex() == 1 ? "男" : "女");
            json.put("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            json.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
            json.put("medicalNumber", patient.getMedicalNumber());
            view.addObject("patient", json);
            JSONArray items = new JSONArray();
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (String data : datas) {
                Long id = Long.valueOf(data.substring(0, data.indexOf("-")));
                Product product = mongoProductRepository.findOne(id);
                if (product != null) {
                    String instructions = data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"));
                    if (StringUtils.isNotBlank(instructions)) {
                        product.setInstructions(instructions);
                    }
                    String quantity = data.substring(data.lastIndexOf("-") + 1, data.indexOf("$"));
                    if (StringUtils.isNotBlank(quantity)) {
                        product.setQuantity(Integer.parseInt(quantity));
                    }
                    BigDecimal price = new BigDecimal(data.substring(data.indexOf("$") + 1));
                    totalPrice = totalPrice.add(price);
                    items.add(JSON.toJSON(product));
                }
            }
            json.put("totalPrice", totalPrice);
            view.addObject("items", items);
        }
        return view;
    }

    /**
     * 打印注射药物处方
     */
    @GetMapping("/printInject")
    public ModelAndView printInject(@RequestParam("username") String username, @RequestParam("patientId") Long patientId, @RequestParam String[] datas) {
        ModelAndView view = new ModelAndView("printInjectPrescription");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            JSONObject json = new JSONObject();
            json.put("name", patient.getName());
            json.put("username", username);
            json.put("sex", patient.getSex() == 1 ? "男" : "女");
            json.put("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            json.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
            json.put("medicalNumber", patient.getMedicalNumber());
            view.addObject("patient", json);
            JSONArray items = new JSONArray();
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (String data : datas) {
                Long id = Long.valueOf(data.substring(0, data.indexOf("-")));
                String instructions = data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"));
                Product product = mongoProductRepository.findOne(id);
                if (StringUtils.isNotBlank(instructions)) {
                    product.setInstructions(instructions);
                }
                String quantity = data.substring(data.lastIndexOf("-") + 1, data.lastIndexOf("$"));
                if(StringUtils.isNotBlank(quantity)){
                    product.setQuantity(Integer.parseInt(quantity));
                }
                BigDecimal price = new BigDecimal(data.substring(data.indexOf("$") + 1));
                totalPrice = totalPrice.add(price);
                items.add(JSON.toJSON(product));
            }
            json.put("totalPrice", totalPrice);
            view.addObject("items", items);
        }
        return view;
    }

    /**
     * 打印毒麻药处方
     *
     * @param patientId
     * @param datas
     * @return
     */
    @GetMapping("/printToxic")
    public ModelAndView printToxic(@RequestParam("username") String username, @RequestParam("patientId") Long patientId, @RequestParam String[] datas) {
        ModelAndView view = new ModelAndView("printToxicPrescription");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            JSONObject json = new JSONObject();
            json.put("name", patient.getName());
            json.put("username", username);
            json.put("sex", patient.getSex() == 1 ? "男" : "女");
            json.put("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            json.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
            json.put("medicalNumber", patient.getMedicalNumber());
            view.addObject("patient", json);
            JSONArray items = new JSONArray();
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (String data : datas) {
                Long id = Long.valueOf(data.substring(0, data.indexOf("-")));
                Product product = mongoProductRepository.findOne(id);
                if (product != null) {
                    String instructions = data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"));
                    if (StringUtils.isNotBlank(instructions)) {
                        product.setInstructions(instructions);
                    }
                    String quantity = data.substring(data.lastIndexOf("-") + 1, data.indexOf("$"));
                    if (StringUtils.isNotBlank(quantity)) {
                        product.setQuantity(Integer.parseInt(quantity));
                    }
                    BigDecimal price = new BigDecimal(data.substring(data.indexOf("$") + 1));
                    totalPrice = totalPrice.add(price);
                    items.add(JSON.toJSON(product));
                }
            }
            json.put("totalPrice", totalPrice);
            view.addObject("items", items);
        }
        return view;
    }

    @GetMapping("/printProjectDisposal")
    public ModelAndView printProjectDisposal(@RequestParam("username") String username, @RequestParam("patientId") Long patientId, @RequestParam String[] datas) {
        ModelAndView view = new ModelAndView("printProjectDisposal");
        Patient patient = patientService.findOne(patientId);
        if (patient != null) {
            JSONObject json = new JSONObject();
            json.put("username", username);
            json.put("name", patient.getName());
            json.put("sex", patient.getSex() == 1 ? "男" : "女");
            json.put("age", DateUtils.getAge(patient.getBirthday(), new Date()));
            json.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
            json.put("medicalNumber", patient.getMedicalNumber());
            BigDecimal totalPrice = BigDecimal.ZERO;
            JSONArray items = new JSONArray();
            for (String data : datas) {
                Long id = Long.valueOf(data.substring(0, data.indexOf("-")));
                String useMethod = data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"));
                String totalSize = data.substring(data.lastIndexOf("-") + 1, data.indexOf("$"));
                BigDecimal price = new BigDecimal(data.substring(data.indexOf("$") + 1));
                totalPrice = totalPrice.add(price);
                Project project = mongoProjectRepository.findOne(id);
                JSONObject jo = (JSONObject) JSON.toJSON(project);
                jo.put("useMethod", useMethod);
                jo.put("totalSize", totalSize);
                items.add(jo);
            }
            json.put("totalPrice", totalPrice);
            view.addObject("patient", json);
            view.addObject("items", items);
        }
        return view;
    }

    @RequestMapping(value = "/lifeCareAdvice", method = RequestMethod.GET)
    public ModelAndView printLifeCareAdvice(@RequestParam Long id) {
        ModelAndView view = new ModelAndView("printLifeCareAdvice");
        LifeCareAdvice advice = lifeCareAdviceService.findOne(id);
        if (advice != null) {
            Long complainedId = advice.getComplaintId();//主诉
            Patient patient = patientService.findOne(advice.getPatientId());
            if (patient != null) {
                Date birthday = patient.getBirthday();
                int age = DateUtils.getAge(birthday, new Date());
                view.addObject("age", age);
                view.addObject("patient", patient);
                try {
                    view.addObject("birthday", DateUtils.getDateStr(birthday));
                } catch (Exception e) {
                    logger.info("打印生活和护理上建议生日有错：" + birthday);
                }
            }
            view.addObject("medicalId", patient.getMedicalNumber());
            // TODO 皮肤分类
//                List<SkinType> SkinTypes = skinTypeService.findByComplainedId(complainedId);
//                if (!SkinTypes.isEmpty()) {
//                    SkinType skinType = SkinTypes.get(0);
//                    String type = skinTypeService.getType(skinType);
//                    view.addObject("skinType", type);
//                }
            Date creationTime = advice.getCreationTime();
            view.addObject("creationTime", DateUtils.getDateStr(creationTime));
            String doctorSuggestIds = advice.getDoctorSuggestId();//医师建议ID集合
            logger.info("医师建议的ID集合：" + doctorSuggestIds);
            if (StringUtils.isNotEmpty(doctorSuggestIds)) {
                JSONArray idsArr = JSON.parseArray(doctorSuggestIds);
                List<Long> ids = new ArrayList<>();
                for (Object oid : idsArr) {
                    ids.add(((Integer) oid).longValue());
                }
                List<Suggest> suggests = suggestRepository.findByIdIn(ids);
                view.addObject("suggests", suggests);
            }
            String supplementAdvice = advice.getSupplementAdvice();
            if (StringUtils.isNotEmpty(supplementAdvice)) {
                view.addObject("supplementAdvice", supplementAdvice);
            }
        }
        return view;
    }

    @RequestMapping(value = "/physiological", method = RequestMethod.GET)
    public ModelAndView printPhysiological(@RequestParam Long id) {
        ModelAndView view = new ModelAndView("printPhysiological");
        PhysiologicalData data = physiologicalService.findOne(id);
        if (data != null && data.getPatientId() != null) {
            Patient patient = patientService.findOne(data.getPatientId());
            if (patient != null) {
                int age = DateUtils.getAge(patient.getBirthday(), new Date());
                view.addObject("patientName", patient.getName());
                view.addObject("patientAge", age);
                view.addObject("sex", patient.getSex());
            }
            view.addObject("medicalId", patient.getMedicalNumber());
            String cleanFace = data.getCleanFace();
            Date creationTime = data.getCreationTime();
            view.addObject("creationTime", DateUtils.getDateStr(creationTime));
            if (cleanFace.contains("卸妆油")) {
                view.addObject("cleanFace1", true);
            }
            if (cleanFace.contains("起泡洗面奶")) {
                view.addObject("cleanFace2", true);
            }
            if (cleanFace.contains("无泡洗面奶")) {
                view.addObject("cleanFace3", true);
            }
            if (cleanFace.contains("温水")) {
                view.addObject("cleanFace4", true);
            }
            if (cleanFace.contains("冷水")) {
                view.addObject("cleanFace5", true);
            }
            view.addObject("data", data);
        }
        return view;
    }

    @RequestMapping(value = "/printRefund", method = RequestMethod.GET)
    public ModelAndView printRefundResearch(@RequestParam Long id) {
        ModelAndView view = new ModelAndView("printRefund");
        RefundRecord record = refundRecordService.findOne(id);
        view.addObject("one", record);
        return view;
    }

}
